// JavaScript Document
var percentageHeightBlockImg = 100;
var percentageHeightBlockText = 100;
var percentageHeightBlockWrapCivette = 39;
var widthWrapCivette = 680;
var indexSlideHome = 0;
var imgBgCorrente = ""
var imgBgOverCorrente = "";
var widthBlockImg = 0;
var heightBlockImg = 0;
var heightCursorSlide = 19;
var intervallSlideHome = "";
var nSlide = 0;
var offsetTopSlide = 30;

$(document).ready(function(){
	
	if(window.orientation == 0 || window.orientation == 180)
	{
		percentageHeightBlockImg = 100;
		percentageHeightBlockText = 100;
		percentageHeightBlockWrapCivette = 32;
	}
	else
	{
		percentageHeightBlockImg = 100;
		percentageHeightBlockText = 100;
		percentageHeightBlockWrapCivette = 0;
	}
		
	goSlideHome(indexSlideHome,false);
	setIntervallSlideHome();
	//openCivette($(".bt_minimize_civette_home"));
	
	$(window).bind('orientationchange',function(){
		
		clearInterval(intervallSlideHome);
		intervallSlideHome = "";
		
		if(window.orientation == 0 || window.orientation == 180)
		{
			percentageHeightBlockImg = 56;
			percentageHeightBlockText = 12;
			percentageHeightBlockWrapCivette = 32;
		}
		else
		{
			percentageHeightBlockImg = 61;
			percentageHeightBlockText = 61;
			percentageHeightBlockWrapCivette = 39;
		}
		
		setDimensionTemplateSlide();
		setDimensionBgSlideHome(true);
		setDimensionBgOverSlideHome(true);
	});
	//attacco il loader sull'immagine appoggio per lo sfondo dello slider
	$('.img_appoggio_bg_slide_home').bind('load',function(){
		
		setDimensionBgSlideHome(false);
		
	});
	//attacco il loader sull'immagine appoggio per l'over dello sfondo
	$(".img_appoggio_over_slide").bind('load',function(){
		
		setDimensionBgOverSlideHome(false);
		
	});
	
	$(".bt_minimize_civette_home").bind('click',function(){
		
		openCivette($(this));
		
	});
	
	$(".bg_slide .loader").css('left',"+="+(widthMenu/2)+'px');
});

$(window).resize(function(){
	
	setDimensionTemplateSlide();
	setDimensionBgSlideHome(true);
	setDimensionBgOverSlideHome(true);
	
});

function startIntervallSlideHome()
{
	indexSlideHome++;
	if(indexSlideHome > (nSlide-1))
		indexSlideHome = 0;
		
	goSlideHome(indexSlideHome,false);
}

function setIntervallSlideHome()
{
	nSlide = $(".slide_home").length;
	intervallSlideHome = setInterval('startIntervallSlideHome()',6000);
}

function setDimensionTemplateSlide()
{
	$(".jspDrag").hide();
	$(".bg_slide").width(widthMenu+widthContentTemplate).height(window_height).css('left','-'+widthMenu+'px');
	$(".cursor_slide_home").css('margin-top',"-"+(((parseInt($(".cursor_slide_home a").length,10)*heightCursorSlide)+5)/2)+'px');
	
	$(".slide_home_wrap").width(widthContentTemplate);
	$(".slide_home_wrap").height(((window_height-HEIGHT_FOOTER)-offsetTopSlide));
	
	if(window.orientation == 0 || window.orientation == 180)
	{	
		heightBlockImg = Math.floor((((window_height-HEIGHT_FOOTER)-offsetTopSlide)*percentageHeightBlockImg)/100);
		widthBlockImg = widthContentTemplate;
		$(".box_img_slide").width(widthBlockImg).height(heightBlockImg).css({top: '0px', right: '0px'});
		
		var heightBlockText = Math.floor((((window_height-HEIGHT_FOOTER)-offsetTopSlide)*percentageHeightBlockText)/100);
		$(".box_txt_slide").width(widthContentTemplate).height(heightBlockText).css('top',heightBlockImg+'px');;
		$(".box_txt_slide .spacer_txt").height(heightBlockText);	
		$(".box_txt_slide .spacer_txt img").height(heightBlockText);
		$(".box_txt_slide").css('text-align','center');	
		//$(".box_txt_slide .title_slide").css('margin-left','0px');		
		//$(".box_txt_slide .title_slide").addClass('portrait');
		
		var heightBlockWrapCivette = Math.floor((((window_height-HEIGHT_FOOTER)-offsetTopSlide)*percentageHeightBlockWrapCivette)/100);
		$(".box_wrap_civette").width(widthContentTemplate).height(heightBlockWrapCivette).css({left: '0px', top: (heightBlockImg+heightBlockText)+'px'});	
		
		$(".cursor_slide_home").css("visibility","visible");
	}
	else
	{
		heightBlockImg = Math.floor((((window_height-HEIGHT_FOOTER)-offsetTopSlide)*percentageHeightBlockImg)/100);
		var percentageWidthImage = 35;
		if(isMobile)
			percentageWidthImage = 32;
			
		widthBlockImg = Math.floor((widthContentTemplate*percentageWidthImage)/100);
		
		$(".box_img_slide").width(widthBlockImg).height(heightBlockImg).css({left: (widthContentTemplate-widthBlockImg)+'px', top: '0px'});	
		
		var heightBlockText = Math.floor((((window_height-HEIGHT_FOOTER)-offsetTopSlide)*percentageHeightBlockText)/100);
		$(".box_txt_slide").width(Math.floor(widthContentTemplate-widthBlockImg)).height(heightBlockText).css({right: '0px', top: '0px'});	
		$(".box_txt_slide .spacer_txt").height(heightBlockText);	
		$(".box_txt_slide .spacer_txt img").height(heightBlockText);
		$(".box_txt_slide").css('text-align','left');
		//$(".box_txt_slide .title_slide").removeClass('portrait');		
		var marginLeftText = ((widthContentTemplate - widthWrapCivette)/2);
		//$(".box_txt_slide .title_slide").css('margin-left',marginLeftText+'px');		
		
		
		var heightBlockWrapCivette = Math.floor((((window_height-HEIGHT_FOOTER)-offsetTopSlide)*percentageHeightBlockWrapCivette)/100);
		$(".box_wrap_civette").width(widthContentTemplate).height(heightBlockWrapCivette).css({left: '0px', top: heightBlockImg+'px'});	
		
		$(".cursor_slide_home").css("visibility","visible");
		/*
		heightBlockImg = Math.floor(((heightContentScroller-offsetTopSlide)*percentageHeightBlockImg)/100);
		widthBlockImg = Math.floor(widthContentTemplate/2);
		$(".box_img_slide").width(widthBlockImg).height(heightBlockImg).css({left: (widthContentTemplate*0.5)+'px', top: '0px'});	
		
		var heightBlockText = Math.floor(((heightContentScroller-offsetTopSlide)*percentageHeightBlockText)/100);
		$(".box_txt_slide").width(Math.floor(widthContentTemplate/2)).height(heightBlockText).css({left: '0px', top: '0px'});	
		$(".box_txt_slide .spacer_txt").height(heightBlockText);	
		$(".box_txt_slide .spacer_txt img").height(heightBlockText);
		$(".box_txt_slide").css('text-align','left');
		$(".box_txt_slide .title_slide").removeClass('portrait');		
		var marginLeftText = ((widthContentTemplate - widthWrapCivette)/2);
		$(".box_txt_slide .title_slide").css('margin-left',marginLeftText+'px');		
		
		
		var heightBlockWrapCivette = Math.floor(((heightContentScroller-offsetTopSlide)*percentageHeightBlockWrapCivette)/100);
		$(".box_wrap_civette").width(widthContentTemplate).height(heightBlockWrapCivette).css({left: '0px', top: heightBlockImg+'px'});	
		*/
	}
	
}

function goSlideHome(index,fromClick)
{		
	if(fromClick == undefined || fromClick == null)
		fromClick = false;
	
	if(fromClick)	
	{
		clearInterval(intervallSlideHome);
		intervallSlideHome = "";
		//setIntervallSlideHome();
	}
		
	$(".bg_slide .loader").show();
	
	imgBgCorrente = $(".slide_home").eq(index).attr('bg');
	
	$('.img_appoggio_bg_slide_home').attr('src',imgBgCorrente);
	imgBgOverCorrente = $('.slide_home').eq(index).find('.box_img_slide').attr('img');
	if(imgBgOverCorrente != "")
		$(".img_appoggio_over_slide").attr('src',$('.slide_home').eq(index).find('.box_img_slide').attr('img'));
	else
		$(".img_appoggio_over_slide").attr('src','');
	
	
		
	$(".cursor_slide_home a.active").removeClass('active');
	$(".cursor_slide_home").find('a').eq(index).addClass('active');
	
	indexSlideHome = index;
	setDimensionTemplateSlide();
}

var chekImgPassaggio = "";
function setDimensionBgSlideHome(fromResize)
{	
	if(fromResize == undefined || fromResize == null)
		fromResize = false;
	
	if(!fromResize)
	{
		if(chekImgPassaggio == imgBgCorrente)
		{
			return;
		}
		
		chekImgPassaggio = imgBgOverCorrente;
	}
	
	var indexBg = indexSlideHome%2;
	
	if(fromResize)
	{
		var widthBgOriginal = parseInt($('.bg_slide_'+indexBg).width(),10);
		var HeightBgOriginal = parseInt($('.bg_slide_'+indexBg).height(),10);
	}
	else
	{
		var widthBgOriginal = parseInt($('.img_appoggio_bg_slide_home').width(),10);
		var HeightBgOriginal = parseInt($('.img_appoggio_bg_slide_home').height(),10);
	}
	
	var newWidthBg = (widthBgOriginal*(window_height-HEIGHT_FOOTER))/HeightBgOriginal;
	var newHeightBg = ((window_height-HEIGHT_FOOTER)-offsetTopSlide);
	
	if(newWidthBg < (widthMenu+widthContentTemplate))
	{
		var newWidthBg = widthMenu+widthContentTemplate;
		var newHeightBg = (HeightBgOriginal*newWidthBg)/widthBgOriginal;
	}
	
	if(newHeightBg < (window_height-HEIGHT_FOOTER))
	{
		var newHeightBg = window_height-HEIGHT_FOOTER;
		var newWidthBg = ( widthBgOriginal*newHeightBg)/HeightBgOriginal;
	}
	
	if(newHeightBg > (window_height-HEIGHT_FOOTER))
		$('.bg_slide_'+indexBg).css('margin-top','-'+((newHeightBg-(window_height-HEIGHT_FOOTER))/2)+'px');
	else
		$('.bg_slide_'+indexBg).css('margin-top','auto');
	
	if(newWidthBg > (widthMenu+widthContentTemplate))
		$('.bg_slide_'+indexBg).css('margin-left','-'+((newWidthBg-(widthMenu+widthContentTemplate))/2)+'px');	
	else
		$('.bg_slide_'+indexBg).css('margin-left','auto');	
	
	if(fromResize)
		$('.bg_slide_'+indexBg).width(newWidthBg).height(newHeightBg);
	else
	{
		
		if(!fromResize)
		{
			
			$(".slide_home.active").fadeOut(function(){
				$(".slide_home").eq(indexSlideHome).fadeIn();
				$(".slide_home.active").removeClass('active');
				$(".slide_home").eq(indexSlideHome).addClass('active');
			});
			
			if(indexBg == 0)
			{
				$('.bg_slide_1').fadeOut(function(){ 
				
					$('.bg_slide_1').css('visibility','hidden');
					$('.bg_slide_1').removeAttr('src');
					$('.bg_slide_'+indexBg).attr('src',imgBgCorrente).width(newWidthBg).height(newHeightBg).hide().css('visibility','visible').fadeIn();
				
				});
			}
			else
			{	
				$('.bg_slide_0').fadeOut(function(){ 
					$('.bg_slide_0').css('visibility','hidden') 
					$('.bg_slide_0').removeAttr('src');
					$('.bg_slide_'+indexBg).attr('src',imgBgCorrente).width(newWidthBg).height(newHeightBg).hide().css('visibility','visible').fadeIn();	
				});
			}
			
			$(".bg_slide .loader").hide();
			
			$('.img_appoggio_bg_slide_home').removeAttr('src');
		}
	}

}

function setDimensionBgOverSlideHome(fromResize)
{
	if(fromResize == undefined || fromResize == null)
		fromResize = false;
	
	if(fromResize)
	{
		var widthBgOriginal = parseInt($('.slide_home').eq(indexSlideHome).find('.box_img_slide').find('.img_over_slide').width(),10);
		var HeightBgOriginal = parseInt($('.slide_home').eq(indexSlideHome).find('.box_img_slide').find('.img_over_slide').height(),10);
	}
	else
	{
		var widthBgOriginal = parseInt($('.img_appoggio_over_slide').width(),10);
		var HeightBgOriginal = parseInt($('.img_appoggio_over_slide').height(),10);
	}
	
	var newWidthBg = widthBlockImg;
	var newHeightBg = (HeightBgOriginal*newWidthBg)/widthBgOriginal;
	
	if(newHeightBg > heightBlockImg)
	{	
		var newHeightBg = heightBlockImg;
		var newWidthBg = (widthBgOriginal * newHeightBg)/HeightBgOriginal;
	}
	
	if(newHeightBg < heightBlockImg)
		$('.slide_home').eq(indexSlideHome).find('.box_img_slide').find('.img_over_slide').css('margin-top','+'+((heightBlockImg-newHeightBg)/2)+'px');	
	
	//alert("newWidthBg:"+newWidthBg+"   widthBlockImg:"+widthBlockImg );
	if(newWidthBg < widthBlockImg)
		$('.slide_home').eq(indexSlideHome).find('.box_img_slide').find('.img_over_slide').css('margin-left','+'+((widthBlockImg-newWidthBg)/2)+'px');	
	
	if(fromResize)
		$('.slide_home').eq(indexSlideHome).find('.box_img_slide').find('.img_over_slide').width(newWidthBg).height(newHeightBg);
	else
		$('.slide_home').eq(indexSlideHome).find('.box_img_slide').find('.img_over_slide').attr('src',imgBgOverCorrente).width(newWidthBg).height(newHeightBg).hide().css('visibility','visible').fadeIn();
	
	if(!fromResize)
	{	
		$('.slide_home').eq(indexSlideHome).find('.box_img_slide').find('.loader').hide();
		
		$('.img_appoggio_over_slide').removeAttr('src');
	}
}

function openCivette(obj)
{
	if(obj.hasClass('open'))
	{
		$('.wrap_civette').animate({
			bottom: '-150px'
		  }, 500, function(){
			  
			obj.removeClass('open')	  
		
		});
		  
		obj.animate({
			bottom: '-1px'
		  }, 500, function(){});
	}
	else
	{
		$('.wrap_civette').animate({
			bottom: '20px'
		  }, 500, function(){
			
			obj.addClass('open')	  
			  
		});
		  
		obj.animate({
			bottom: '170px'
		  }, 500, function(){});
	}	
}
